var Moment = require("../../utils/moment.js");
const dates = new Date()

Page({
  data: {
    title: '日期选择',
    // isShow: true, //按月选择
    // isShows: false, //按日选择
    time_date: true, //显示日
    time_month: false, //显示月份

    picketypelist: [{
        id: '0',
        name: '按日选择'
      },
      {
        id: '1',
        name: '按月选择'
      }
    ],
    picketype: '0', //默认按月选择

    years: [],
    year: dates.getFullYear(),
    months: [],
    month: dates.getMonth() + 1,
    value: [0, dates.getMonth()],

    // isPickerRender: false,
    // isPickerShow: false,
    pickerConfig: {
      endDate: true,
      column: "second",
      dateLimit: false,
      initStartTime: Moment(new Date()).format('YYYY-MM-DD'), //开始日期默认当日
      initEndTime: Moment(new Date()).add(1, 'day').format('YYYY-MM-DD'), //结束日期默认当日+1
      limitStartTime: "2019-01-01", //开始日期最小值
      limitEndTime: "2100-12-31", //结束日期最大值
    },
    frompage: '',
  },

  resetDate: function() {
    console.log('lalalala')
    let temp = this.data.pickerConfig
    console.log('temp', temp)
    temp.initStartTime = Moment(new Date()).format('YYYY-MM-DD')
    temp.initEndTime = Moment(new Date()).add(1, 'day').format('YYYY-MM-DD')
    this.setData({
      pickerConfig: temp
    })
    console.log('pickerConfig', this.data.pickerConfig)
    this.timePicker.initPick(this.data.pickerConfig)
  },

  //选择日期后赋值
  setPickerTime: function(e) {
    console.log(e);
    var pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      startDate: e.detail.startTime,
      FinishDate: e.detail.endTime,
      btn_text: e.detail.startTime + '-' + e.detail.endTime,
      btn_width: '45%'
    })
    if (this.data.frompage == 'baojie') {
      prevPage.setData({
        Starttime: e.detail.startTime,
        Endtime: e.detail.endTime
      })
    }
      wx.navigateBack({
        delta: 1
      })
  },

  bindChange: function(e) { //按日选择
    console.log(e)
    console.log(e.detail.value[1])
    var yue = e.detail.value[1] + 1;
    if (yue < 10) {
      this.setData({
        year: this.data.years[e.detail.value[0]],
        month: '0' + this.data.months[e.detail.value[1]]
      })
    } else {
      this.setData({
        year: this.data.years[e.detail.value[0]],
        month: this.data.months[e.detail.value[1]]
      })
    }
  },

  button: function(e) { //点击选择月/日
    console.log(JSON.stringify(e.currentTarget.dataset.index))
    if (e.currentTarget.dataset.index == 0) {
      this.setData({
        isShow: true,
        isShows: false,
        time_month: false,
        time_date: true,
        picketype: '0'
      })
    } else {
      this.setData({
        isShow: false,
        isShows: true,
        time_date: false,
        time_month: true,
        picketype: '1'
      })
    }
  },

  //按月选择重置
  reset() {
    let val = this.data.value
    for (var i = 0; i < this.data.years.length; i++) {
      if (this.data.years[i] == dates.getFullYear()) {
        val[0] = i
        this.setData({
          value: val
        })
        break;
      }
    }
  },

  //按月选择确定
  confirm() {
    console.log(this.data.year, this.data.month, dates.getMonth())

    var lastDay = new Date(this.data.year, this.data.month, 0);
    var day = lastDay.getDate();
    day = day < 10 ? '0' + day : day;
    console.log(day + "------------当月天数")


    var pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      startDate: this.data.year + '-' + this.data.month + '-' + '01',
      FinishDate: this.data.year + '-' + this.data.month + '-' + day,
      btn_text: this.data.year + '-' + this.data.month,
      btn_width: '22%'
    })
      wx.navigateBack({
        delta: 1
      })
  },

  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (options.frompage != undefined && options.frompage != '') {
      this.setData({
        frompage: options.frompage
      })
    }
    let val = this.data.value
    let years = this.data.years
    let months = this.data.months
    for (let i = 2019; i <= 2100; i++) {
      years.push(i)
    }
    for (let i = 1; i <= 12; i++) {
      months.push(i)
    }
    this.setData({
      years: years,
      months: months,
    })
    for (var i = 0; i < this.data.years.length; i++) {
      if (this.data.years[i] == dates.getFullYear()) {
        val[0] = i
        this.setData({
          value: val
        })
        break;
      }
    }
    this.timePicker = this.selectComponent("#timePicker")
  },

  onReady: function() {
    // 页面渲染完成
  },

  onShow: function() {

  },

  onHide: function() {
    // 页面隐藏
  },

  onUnload: function() {
    // 页面关闭
  },

})